package com.cc.service.amount;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.List;
import java.util.stream.Collectors;

import com.cc.infrastructure.inter.amount.AmountInter;
import com.cc.web.vo.AmountVo;
import com.cc.infrastructure.db.AmountDb;
import com.cc.pojo.dto.AmountDto;
import com.cc.infrastructure.special.dto.AmountDo;

/**
 * @author C.c
 **/
@Service
public class AmountService {
  @Resource
  private AmountInter amountInter;

  /**
   * 分页
   *
   * @author C.c
   **/
  public Page<AmountVo> query(String name, int pageNum, int pageSize) {
    Page<AmountDo> amountDoDos = amountInter.query(name, pageNum, pageSize);
    List<AmountVo> collect = amountDoDos.stream().map(AmountVo::convertAmountVoByAmountDo).collect(Collectors.toList());
    return new PageImpl<>(collect, amountDoDos.getPageable(), amountDoDos.getTotalElements());
  }

  /**
   * 新增
   *
   * @author C.c
   **/
  @Transactional(rollbackFor = Exception.class)
  public boolean add(AmountDto amountDto) {
    AmountDb amountdb = AmountDto.convertAmountDbByAmountDto(amountDto);
    amountdb.setId(UUID.randomUUID().toString());
    amountInter.save(amountdb);
    return true;
  }

  /**
   * 删除
   *
   * @param id
   * @return boolean
   * @author C.c
   **/
  public boolean delete(String id) {
    amountInter.deleteById(id);
    return true;
  }

  /**
   * 修改
   *
   * @return boolean
   * @author C.c
   **/
  @Transactional(rollbackFor = Exception.class)
  public boolean edit(AmountDto amountDto) {
    AmountDb amountdb = AmountDto.convertAmountDbByAmountDto(amountDto);
    amountInter.save(amountdb);
    return true;
  }

  /**
   * 查询单个
   *
   * @param id 唯一标识
   * @author C.c
   **/
  public AmountVo view(String id) {
    AmountDb amountdb = amountInter.findById(id).orElse(null);
    return AmountVo.convertAmountVoByAmountDo(amountdb);
  }
}